/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package vista;

import controlador.Utilidades;

import controlador.servicios.ServicioCuota;
import controlador.servicios.ServicioCuotaCobrarPagar;

import javax.swing.JOptionPane;
import vista.modeloTablas.ModeloTablaCoutasPagarCobrar;

import vista.modeloTablas.ModeloTablaCuotas;


/**
 *
 * @author Alex
 */
public class PanelAdministrarCuotas extends javax.swing.JPanel {
    private ServicioCuota sc = new ServicioCuota();
    private ServicioCuotaCobrarPagar scp = new ServicioCuotaCobrarPagar();
    private ModeloTablaCuotas modeloCuotas = new ModeloTablaCuotas();
    private ModeloTablaCoutasPagarCobrar modelo = new ModeloTablaCoutasPagarCobrar();
    /**
     * Creates new form PanelAdministrarMarcas
     */
    public PanelAdministrarCuotas() {
        initComponents();
        cargarTabla();
    }
    private void cargarTabla(){
        modelo.setLista(scp.listar());
        tbl_cuentas.setModel(modelo);
        tbl_cuentas.updateUI();
        tbl_cuotas.setModel(modeloCuotas);
        tbl_cuotas.updateUI();
    }
    private void limpiarCampos(){
        scp.nuevaInstancia();
        sc.nuevaInstancia();
    }
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbl_cuentas = new javax.swing.JTable();
        btnCancelarD1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbl_cuotas = new javax.swing.JTable();
        btnCancelarD = new javax.swing.JButton();
        btnModificarMarca = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Administrar Categorias"));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Listado de cuentas"));

        tbl_cuentas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Numero", "Nombre de Marca"
            }
        ));
        jScrollPane2.setViewportView(tbl_cuentas);

        btnCancelarD1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/images/reload.png"))); // NOI18N
        btnCancelarD1.setText("Ver cuotas");
        btnCancelarD1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarD1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnCancelarD1, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 116, Short.MAX_VALUE)
                .addComponent(btnCancelarD1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(45, Short.MAX_VALUE)))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Listado de Cuotas"));

        tbl_cuotas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Numero", "Nombre de Marca"
            }
        ));
        jScrollPane1.setViewportView(tbl_cuotas);

        btnCancelarD.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/images/reload.png"))); // NOI18N
        btnCancelarD.setText("Salir");

        btnModificarMarca.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vista/images/edit.png"))); // NOI18N
        btnModificarMarca.setText("Pagar");
        btnModificarMarca.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModificarMarcaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnModificarMarca, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnCancelarD, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(13, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelarD, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnModificarMarca, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnModificarMarcaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModificarMarcaActionPerformed
        // TODO add your handling code here:
        int fila = tbl_cuotas.getSelectedRow();
        limpiarCampos();
        if(fila >=0){
            sc.fijarInstancia(modeloCuotas.getLista().get(fila));
            if(sc.getCuota().isPagado()==true){
                JOptionPane.showMessageDialog(this, "La cuota ya esta cancelada");
            }else{
                int pagado = JOptionPane.showConfirmDialog(this,"Desea pagar la cuenta");
                if(pagado == JOptionPane.OK_OPTION){
                    sc.getCuota().setPagado(true);
                    if(sc.guardar()==true){
                        JOptionPane.showMessageDialog(this, "Se ha cancelado la cuota");
                        cargarTabla();
                    }else{
                        JOptionPane.showMessageDialog(this, "No se pudo cancelar");
                    }
                }
            }
        }else{
            JOptionPane.showMessageDialog(this, "Escoja un dato de la tabla", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnModificarMarcaActionPerformed

    private void btnCancelarD1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarD1ActionPerformed
        // TODO add your handling code here:
        int fila = tbl_cuentas.getSelectedRow();
        limpiarCampos();
        if(fila >=0){
            scp.fijarInstancia(modelo.getLista().get(fila));
            modeloCuotas.setLista(scp.getCuota().getListaCuotas());
            tbl_cuotas.setModel(modeloCuotas);
            tbl_cuotas.updateUI();
        }else{
            JOptionPane.showMessageDialog(this, "Escoja un dato de la tabla", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnCancelarD1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelarD;
    private javax.swing.JButton btnCancelarD1;
    private javax.swing.JButton btnModificarMarca;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tbl_cuentas;
    private javax.swing.JTable tbl_cuotas;
    // End of variables declaration//GEN-END:variables
}
